﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Com.CMS263.BLL;
using Com.CMS263.PO;
using Com.CMS263.Util;

namespace Com.CMS263.Web.manage
{
    public partial class modArt : System.Web.UI.Page
    {   ArticleBLL abll = new ArticleBLL();
        ArtTypeBLL atbll = new ArtTypeBLL();
        public string imgPath = null;
        public int id;
        Com.CMS263.PO.ArtType type = null;
        protected void Page_Load(object sender, EventArgs e)
        {
         
       
           
            if (!this.IsPostBack)
            {
                 int page = 1;
                id = int.Parse(Request["id"]);

                //根据父类编号取得单条信息以修改
                Article art = abll.GetArticleById(id);
                page = string.IsNullOrEmpty(Request["page"]) ? 1 : int.Parse(Request["page"]);
                this.hdfPage.Value = page.ToString();
                this.BindArticle(art);

                id = int.Parse(Request["id"]);
                type = atbll.GetArtTypeById(art.typeID);
               
                this.lbTypeName.Text = type.typeName;
                this.hdfTypeId.Value = id.ToString();
              
            }


        }

        private void BindArticle(Article art)
        {
            if (art.title.IndexOf("<b>") > 0)
            {
                this.chkStrong.Checked = true;
            }
            if (art.title.IndexOf("color:red")>0)
            {
                this.chkRed.Checked = true;
            }
            imgPath = art.artImg;
            this.txtAddDate.Text = art.addDate.ToString();
            this.txtRemark.Text = art.remark;
            this.txtAddDate.Text = art.addDate.ToString();
            this.txtTitle.Text = art.title.Replace("<span style='color:red'>","").Replace("</span>","").Replace("<b>","").Replace("</b>","");
            this.con.Value = art.content;
            this.hdfTypeId.Value = id.ToString();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Article article = abll.GetArticleById(int.Parse(this.hdfTypeId.Value));
            string title = this.txtTitle.Text;
            string remark = this.txtRemark.Text;
            string con = this.con.Value;
            string addDate = this.txtAddDate.Text;

            if (chkStrong.Checked)
            {
                title = "<b>" + title + "</b>";

            }
            if (this.chkRed.Checked)
            {
                title = "<span style='color:red'>" + title + "</span>";
            }

            if (article.typeID == 42)
            {
                if (this.fuImg.FileContent.Length > 5)
                {
                    string fileName = fuImg.FileName.Split('.')[1];
                    fileName = DateTime.Now.ToString("yyyyMMddhhssmmmm") + (new Random()).Next(1000).ToString() + "." + fileName;
                    fuImg.SaveAs(Server.MapPath("/upfile/" + fileName));
                    article.artImg = fileName;
                }
            }
            article.title = title;
           
            article.remark = remark;

            article.addDate = DateTime.Parse(addDate);
          
          
            article.content = con;
            abll.ModifyArt(article);
         
            StringUtil.Alert("修改成功", "ArticleList.aspx?pid=" +article.typeID+"&page="+this.hdfPage.Value);
       
          
            
        }
        }
    }

